package com.pda.xinma.ui.handingin;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.pda.xinma.R;
import com.pda.xinma.adapter.HandingInListAdapter;
import com.pda.xinma.bean.HandingInDetailBean;
import com.pda.xinma.bean.HandingInListBean;
import com.pda.xinma.bean.ScanBean;
import com.pda.xinma.contract.HandingInWaitContract;
import com.pda.xinma.presenter.HandingInWaitPresenter;
import com.pda.xinma.view.ScanButView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class HandingInWaitFragment extends BaseMvpFragment<HandingInWaitPresenter> implements HandingInWaitContract.View, OnRefreshListener, OnLoadMoreListener {
    HandingInListAdapter handingInListAdapter;
    @BindView(R.id.rv_content)
    RecyclerView rvContent;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    private int pager = 1;

    @Override
    public HandingInWaitPresenter getPresenter() {
        return new HandingInWaitPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_list_scan;
    }

    @Override
    public void initView() {
        handingInListAdapter = new HandingInListAdapter(R.layout.item_hand_list, new ArrayList<>());
        rvContent.setAdapter(handingInListAdapter);
        srlLayout.setEnableRefresh(true);
        srlLayout.setEnableLoadMore(true);
        srlLayout.setOnRefreshListener(this);
        srlLayout.setOnLoadMoreListener(this);
        sbScan.setVisibility(View.VISIBLE);
    }

    @Override
    public void onResume() {
        super.onResume();
        srlLayout.autoRefresh();
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                String content=scanBean.getCode();
                if (content.contains(",")){
                    String[] parts = content.split(",");
                    HandingInDetailBean handingInDetailBean=new HandingInDetailBean();
                    try {
                        handingInDetailBean.setThirdOrderNo(parts[0]);
                        handingInDetailBean.setBatch(parts[1]);
                        handingInDetailBean.setProductModel(parts[2]);
                        handingInDetailBean.setIndexNo(parts[3]);
                        handingInDetailBean.setWeight(parts[4]);
                        handingInDetailBean.setApparentDensity(parts[5]);
                        handingInDetailBean.setLiquidity(parts[6]);
                        handingInDetailBean.setProduceDate(parts[7]);
                        handingInDetailBean.setShelfLifeDate(parts[8]);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("BEAN", handingInDetailBean);
                    startIntent(AppActivityKey.HANDINGINTABACTIVITY, bundle);
                }else {
                    showMessage("请扫正确的码");
                }
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        sbScan.setScanCallBack(null);
    }

    @Override
    public void initLinsenterner() {
        handingInListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("entryNo", handingInListAdapter.getData().get(position).getEntryNo());
                bundle.putString("entryNoStatus", handingInListAdapter.getData().get(position).getEntryStatus());
                startIntent(AppActivityKey.HANDINGINTABACTIVITY, bundle);
            }
        });

    }

    @Override
    public void showMessage(String msg) {
        super.showMessage(msg);
        srlLayout.finishRefresh();
        srlLayout.finishLoadMore();
    }


    @Override
    public void getData() {

    }

    @Override
    public void setDataList(List<HandingInListBean> list) {
        srlLayout.finishRefresh();
        srlLayout.finishLoadMore();
        if (pager == 1) {
            handingInListAdapter.setNewInstance(list);
        } else {
            handingInListAdapter.addData(list);
        }

    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        pager = 1;
        presenter.selectEntryList("10", "", pager);
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        pager++;
        presenter.selectEntryList("10", "", pager);
    }
}
